package com.bop.appender;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author: LiFuqiang
 * @Date: 2022/1/21 16:30
 * @Version 1.0
 * @description
 */
public class HdfsAppenderLogCache {
    private BlockingQueue<byte[]> queue = null;

    private long size = 0L;

    private long batchSize = 0L;

    public HdfsAppenderLogCache() {
        this.queue = new LinkedBlockingQueue<>();
        this.size = 4096;
        this.batchSize = (long) (this.size * 0.8);
    }

    public synchronized void add(byte[] event) {
        this.queue.offer(event);
    }

    public List<byte[]> drain() {
        List<byte[]> events = new LinkedList<>();
        this.queue.drainTo(events);
        return events;
    }

    public BlockingQueue<byte[]> getQueue() {
        return queue;
    }

    public void setQueue(BlockingQueue<byte[]> queue) {
        this.queue = queue;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getBatchSize() {
        return batchSize;
    }

    public void setBatchSize(long batchSize) {
        this.batchSize = batchSize;
    }

}
